import {messages} from "share/common";
import React, { Component } from 'react'
import AdvancedTable from "components/advanced-table";
import DataHoc from "../pay-check/data-hoc";
import httpFetch from "share/httpFetch";
import { message, Modal, Form, Input, Spin, Button } from 'antd'

class WaitRecheck extends Component {

  key = 'wait-recheck';

  onSelectChange = selectedRowKeys => {
    let { rowSelection } = this.state;
    rowSelection.selectedRowKeys = selectedRowKeys;
    this.setState({ rowSelection });
  };

  onSelectItem = (record, selected) => {
    const { selectedData } = this.state;
    if (selected) {
      selectedData.push(record);
    } else {
      selectedData.splice(selectedData.findIndex(item => item.id === record.id),1);
    }
    this.setState({ selectedData });
  };

  onSelectAll = (selected, selectedRows, changeRows) => {
    changeRows.map(changeRow => this.onSelectItem(changeRow, selected));
  };

  state = {
    loading: false,
    selectedData: [],
    rowSelection: {
      selectedRowKeys: [],
      onChange: this.onSelectChange,
      onSelect: this.onSelectItem,
      onSelectAll: this.onSelectAll
    },
    remark: '',
    searchForm: this.props.getSearchForm(this.key)
  };

  service = (page, pageSize, searchParams) => {
    return httpFetch.post(`/payment/api/cash/bankPay/queryBatch`, { page, size: pageSize, ...searchParams, hasChecked: false, isPreChecked: true });
  };

  handlePass = () => {
    Modal.confirm({
      title: messages('finance-4.key88')/*复核通过？*/,
      content: messages('finance-4.key100')/*复核通过后，待付数据将推送下一步复核，请确认是否需要支付？*/,
      okText: messages('finance-4.key85')/*复核通过*/,
      onOk: this.check
    })
  };

  check = () => {
    const { selectedData } = this.state;
    this.setState({ loading: true });
    httpFetch.post('/payment/api/cash/bankPay/preCheck', {
      checkStatus: '01',
      batchNos: selectedData.map(item => item.batchNo)
    }).then(() => {
      this.setState({ loading: false }, () => {
        this.advancedTableRef.getList();
      });
    }).catch(e => {
      this.setState({ loading: false });
      message.error(e.response.data.message);
    })
  };

  handleReject = () => {
    Modal.confirm({
      title: messages('finance-4.key97')/*复核驳回？*/,
      content: <div>
        {messages('finance-4.key98')/*请确认对支付订单批次进行复核驳回操作，驳回后，可重新在付款工作台中发起支付*/}
        <Form.Item
          label={messages('finance-4.key63')/*备注temp*/}
          labelCol={{ span: 6 }}
          wrapperCol={{ span: 16 }}
          className={'new-form'}
          style={{ margin: '10px 0', padding: 0 }}
        >
          <Input placeholder={messages('finance-4.key70')/*请输入temp2*/} onChange={e => this.setState({ remark: e.target.value })}/>
        </Form.Item>
      </div>,
      okText: messages('finance-4.key99')/*确认*/,
      onOk: () => this.reject()
    })
  };

  reject = () => {
    const { selectedData, remark } = this.state;
    httpFetch.post('/payment/api/cash/bankPay/preCheck', {
      checkStatus: '00',
      batchNos: selectedData.map(item => item.batchNo),
      remark
    }).then(() => {
      this.setState({ loading: false, remark: '' }, () => {
        this.advancedTableRef.getList();
      });
    }).catch(e => {
      this.setState({ loading: false, remark: '' });
      message.error(e.response.data.message);
    })
  };

  render() {
    const { loading, rowSelection, selectedData, searchForm } = this.state;
    const { getColumns } = this.props;
    return (
      <div style={{ height: 'calc(100vh - 110px)' }}>
        <Spin spinning={loading}>
          <AdvancedTable
            title={messages('finance-4.key84')/*待复核*/}
            tableName='wait-check'
            service={this.service}
            rowSelection={rowSelection}
            onRef={ref => this.advancedTableRef = ref}
            searchForm={searchForm}
            rowKey="id"
            columns={getColumns(this.key)}
            footer={<div>
              <Button type='primary' onClick={this.handlePass} disabled={selectedData.length === 0}>{messages('finance-4.key85')/*复核通过*/}</Button>
              <Button onClick={this.handleReject} style={{ marginLeft: '8px' }} disabled={selectedData.length === 0}>{messages('finance-4.key101')/*复核驳回*/}</Button>
            </div>}
          />
        </Spin>
      </div>
    )
  }

}

export default DataHoc(WaitRecheck)
